/**
 *  此js不依赖任何的其它js  可以将其放在js列表中的第一位
 * @returns
 */
const hystore = (function() {
  window.hystore = new hystoreFun();

  function hystoreFun() {
    this.browserScopeChange = function() {
      var cookieBrowserRunFlag = "browser_run";
      if (!window.hystore.isValid(window.hystore.getCookie(cookieBrowserRunFlag))) {
        window.hystore.clearBrowserRunItem();
        window.hystore.setCookie(cookieBrowserRunFlag, "1");
      }
    };
    //请由知道退出的组件来调用此  清楚登录域的数据
    this.clearLoginScope = function() {
      localStorage.removeItem(window.hystore.loginScope());
    };
    this.clearBrowserRunItem = function() {
      localStorage.removeItem(window.hystore.browserRunScope());
      window.hystore.clearLoginScope(); //浏览器重新打开的时候，需要将登录域一起清空
    };
    ////////////////////////////////////监听器定义//////////////////////////////////////////////
    this.browserRunItemOptionListener = function() {
      window.hystore.browserScopeChange();
    };
    this.loginScopeItemOptionListener = function() {
      //登录域被操作的时候，需要给浏览器打开域通知，可能会去清空登录域
      window.hystore.browserScopeChange();
    };
    /////////////////////////////删除定义//////////////////////////////////////////////
    this.removeBrowserRunItem = function(key) {
      window.hystore.browserRunItemOptionListener();
      return window.hystore.doRemoveItem(window.hystore.browserRunScope(), key);
    };
    this.removeForeverScopeItem = function(key) {
      return window.hystore.doRemoveItem(window.hystore.foreverScope(), key);
    };
    this.removeLoginScopeItem = function(key) {
      this.loginScopeItemOptionListener();
      return window.hystore.doRemoveItem(window.hystore.loginScope(), key);
    };
    this.doRemoveItem = function(scope, key) {
      if (!window.hystore.isValid(key)) {
        return null;
      }
      var oldValue = null;
      var data = window.hystore.getScopeData(scope);
      if (window.hystore.isValid(data)) {
        oldValue = data[key];
        if (window.hystore.isValid(oldValue)) {
          data[key] = null;
          delete data[key]; //删除某个字段
          localStorage.setItem(scope, JSON.stringify(data));
          return oldValue;
        }
      }
      return oldValue;
    };
    ////////////////////////////增加及修改定义///////////////////////////////////////////////
    this.setBrowserRunItem = function(key, value) {
      window.hystore.browserRunItemOptionListener();
      return window.hystore.doSetItem(window.hystore.browserRunScope(), key, value);
    };
    this.setForeverScopeItem = function(key, value) {
      return window.hystore.doSetItem(window.hystore.foreverScope(), key, value);
    };
    this.setLoginScopeItem = function(key, value) {
      this.loginScopeItemOptionListener();
      return window.hystore.doSetItem(window.hystore.loginScope(), key, value);
    };
    this.doSetItem = function(scope, key, value) {
      if (!window.hystore.isValid(key) || !window.hystore.isValid(value)) {
        return null;
      }
      var oldValue = null;
      var data = window.hystore.getScopeData(scope);
      if (window.hystore.isValid(data)) {
        oldValue = data[key];
      } else {
        data = {};
      }
      data[key] = JSON.stringify(value);
      localStorage.setItem(scope, JSON.stringify(data));
      return oldValue;
    };
    ////////////////////////////查询定义///////////////////////////////////////////////
    this.getBrowserRunItem = function(key, defVal) {
      window.hystore.browserRunItemOptionListener();
      return window.hystore.doGetItem(window.hystore.browserRunScope(), key, defVal);
    };
    this.getForeverScopeItem = function(key, defVal) {
      return window.hystore.doGetItem(window.hystore.foreverScope(), key, defVal);
    };
    this.getLoginScopeItem = function(key, defVal) {
      this.loginScopeItemOptionListener();
      return window.hystore.doGetItem(window.hystore.loginScope(), key, defVal);
    };

    this.doGetItem = function(scope, key, defVal) {
      // console.log(scope, key, defVal);
      // debugger;
      var loginScope = localStorage.getItem(scope);
      if (!loginScope) {
        if (window.hystore.isValid(defVal)) {
          return defVal;
        }
        return null;
      }
      var loginData = JSON.parse(loginScope);
      for (var field in loginData) {
        if (field == key) {
          return JSON.parse(loginData[field]);
        }
      }
      if (window.hystore.isValid(defVal)) {
        return defVal;
      }
      return null;
    };

    this.getScopeData = function(scope) {
      var loginScope = localStorage.getItem(scope);
      if (!loginScope) {
        return null;
      }
      return JSON.parse(loginScope);
    };
    /////////////////////////////////域key定义//////////////////////////////////////////
    //登录域
    this.loginScope = function() {
      return window.hystore.getPrefix() + "login_scope";
    };
    //浏览器一次开启直到关闭域
    //就是浏览器关闭  则缓存失效
    //缓存在开启浏览器后开始使用 一直存在持续到浏览器关闭
    this.browserRunScope = function() {
      return window.hystore.getPrefix() + "run_scope";
    };
    //永久域
    this.foreverScope = function() {
      return window.hystore.getPrefix() + "forever_scope";
    };
    //支持多环境
    this.getPrefix = function() {
      return "";
    };
    ////////////////////////////////////通用工具定义///////////////////////////////////////
    this.isValid = function(value) {
      if (!value && value != 0) {
        return false;
      }
      return true;
    };
    this.delCookie = function(name) {
      var exp = new Date();
      exp.setTime(exp.getTime() - 1);
      var cval = window.hystore.getCookie(name);
      if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
      }
    };

    //如果需要设定自定义过期时间
    //那么把上面的setCookie函数换成下面两个函数就ok;
    //程序代码
    this.setCookie = function(name, value) {
      document.cookie = name + "=" + escape(value) + ";path=/";
    };
    this.getCookie = function(name) {
      var arr,
        reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
      if ((arr = document.cookie.match(reg))) {
        return unescape(arr[2]);
      } else {
        return null;
      }
    };
    ///////////////////////////////////////////////////////////////////////////
  }
})();

export default hystore;
